﻿using System;
using System.Net;
using System.Configuration;
using System.ServiceProcess;

namespace SBPweb.Communication.Server.PolicyHost
{

    public partial class PolicyService : ServiceBase
    {

        #region Private fields

        private PolicySocketServer pss;

        #endregion

        #region Constructor logic

        public PolicyService()
        {
            InitializeComponent();
            this.ServiceName = "eSSECS Policy Host";
            //<policy-file-request/>

            string hostPublicIp = ConfigurationSettings.AppSettings["socketHostPublicIp"];
            if (string.IsNullOrEmpty(hostPublicIp))
            {
                IPHostEntry hE = Dns.Resolve(Dns.GetHostName());
                hostPublicIp = hE.AddressList[0].ToString();
            }
            pss = new PolicySocketServer(IPAddress.Parse(hostPublicIp), 943);
            pss.ClientRequest += new EventHandler<PolicySocketServer.ClientActionEventArgs>(pss_ClientRequest);
            pss.ClientResponse += new EventHandler<PolicySocketServer.ClientActionEventArgs>(pss_ClientResponse);
        }

        #endregion

        #region Life-cycle

        protected override void OnStart(string[] args)
        {
            pss.StartAsync();
        }

        protected override void OnStop()
        {
            try
            {
                pss.Stop();
            }
            catch(Exception ex)
            {
            }
        }

        #endregion

        #region Event handlers

        private void pss_ClientResponse(object sender, PolicySocketServer.ClientActionEventArgs e)
        {
        }

        private void pss_ClientRequest(object sender, PolicySocketServer.ClientActionEventArgs e)
        {
        }

        #endregion

    }

}